package com.erp.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import ognl.Ognl;
import ognl.OgnlException;

import com.erp.dto.Privilege;
import com.erp.util.PrivilegeUtil;
import com.opensymphony.xwork2.ActionContext;

//自定义标签的规范   该标签的功能:显示所有的学生的详细资料   
//1、继承：javax.servlet.jsp.tagext.TagSupport   
//2、重写方法doEndTag和doStartTag（系统自动调用）   

@SuppressWarnings("serial")
public class TagConvertor extends TagSupport {

	private String url;
	private String value;
	private String params;
	private String onclick;
	private String attr;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getAttr() {
		return attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			String action = this.url;

			// 获得Session
			HttpSession session = this.pageContext.getSession();

			// 获取用户权限列表
			List<Privilege> validPrivilegeList = (List<Privilege>) session
					.getAttribute("userPrivilegeList");
			// 获得Application域的属性值
			List<Privilege> allPrivilegeList = (List<Privilege>) pageContext
					.getServletContext().getAttribute("privilegeList");
			// 向前台输出完整a标签
			if (PrivilegeUtil.isValid(action, validPrivilegeList,
					allPrivilegeList)) {
				// 添加herf属性
				String tagA = "<a href='" + this.url + ".action";
				if (this.params != null && !"".equals(this.params)) {
					tagA += "?" + getFormattedParams(this.params);
				}
				tagA += "' ";

				// 添加点击事件
				if (this.onclick != null && !"".equals(this.onclick)) {
					tagA += "' onclick=\"" + this.onclick + "\" ";
				}

				// 添加自定义属性
				if (this.attr != null && !"".equals(this.attr)) {
					String[] attrArray = attr.split(":");
					tagA += "' " + attrArray[0] + "=\"" + attrArray[1] + "\" ";
				}

				// 结束标签
				tagA += ">" + this.value + "</a>";
				// 输出标签
				out.print(tagA);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return SKIP_BODY; // 不实现标签的体，即空体标签。
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * 根据标签的params属性值获取格式化以后的参数字符串（格式：id=xxx&name=xxx）
	 * 
	 * @param params
	 * @return
	 */
	public String getFormattedParams(String params) {
		String result = "";
		String[] paramsArray = params.split(",");
		int length = paramsArray.length;
		try {
			result += paramsArray[0]
					+ "="
					+ Ognl.getValue(params, ActionContext.getContext()
							.getValueStack().getRoot());
			for (int i = 1; i < length; i++) {
				result += "&"
						+ paramsArray[i]
						+ "="
						+ Ognl.getValue(params, ActionContext.getContext()
								.getValueStack().getRoot());
			}
		} catch (OgnlException e) {
			e.printStackTrace();
		}
		return result;
	}
}